package com.leosoft.heatmonitor.control;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.leosoft.heatmonitor.service.transactional.MeterService;

@Controller
@RequestMapping("/meter")
public class MeterController {
	@Autowired(required = true)
	MeterService meterService;

	String[] columns = { "name", "deviceKey", "addr1", "id" };

	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request) {
		String username = getUserName();
		int startIndex = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String draw = request.getParameter("draw");

		List<String[]> odrerPart = new ArrayList<>();
		List<String[]> wherePart = new ArrayList<>();

		for (int i = 0; i < columns.length; i++) {
			String orderColumn = request.getParameter("order[" + i + "][column]");
			if (orderColumn != null) {
				String orderDir = request.getParameter("order[" + i + "][dir]");
				odrerPart.add(new String[] { columns[Integer.parseInt(orderColumn) - 1], orderDir });
			}
		}

		for (int i = 0; i < columns.length; i++) {
			String searchable = request.getParameter("columns[" + i + "][searchable]");
			if (searchable.equals("true")) {
				String searchColumn = columns[i];
				String value = request.getParameter("columns[" + i + "][search][value]");
				if (!value.isEmpty()) {
					wherePart.add(new String[] { searchColumn, value });
				}
			}
		}

		Map<String, Object> result = meterService.list(startIndex, length, wherePart, odrerPart, username);
		result.put("draw", draw);
		return result;
	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	ModelAndView create(@RequestParam("deviceKey") String deviceKey, @RequestParam("addr1") String addr1,
			@RequestParam("name") String name) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("nav", "meter");
		mv.setViewName("home");
		if (!meterService.create(deviceKey, addr1, name)) {
			mv.addObject("error", "创建总表失败: 重复的总表!");
		}
		return mv;
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	String update(@RequestParam("deviceKey") String deviceKey, @RequestParam("addr1") String addr1,
			@RequestParam("id") String id, @RequestParam("name") String name) {
		meterService.update(id, deviceKey, addr1, name);
		return "redirect:/home/meter";
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	String delete(@RequestParam("id") String id) {
		meterService.delete(id);
		return "redirect:/home/meter";
	}

	String getUserName() {
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String username;
		if (principal instanceof UserDetails) {
			username = ((UserDetails) principal).getUsername();
		} else {
			username = principal.toString();
		}
		return username;
	}
}
